package com.bocloud.http;

import com.alibaba.fastjson.JSONObject;

import org.apache.http.*;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2019/1/21.
 */

@Component
public class HttpClient {

    Logger log = LoggerFactory.getLogger(HttpClient.class);


    @Autowired
    private RequestConfig config;


    @Autowired
    private CloseableHttpClient closeableHttpClient;


    public String doGet(String url, Map<String, String> header, String encoding) throws IOException {
        if (null == encoding) {
            encoding = "utf-8";
        }
        HttpGet httpGet = new HttpGet(url);
        if (null != header && header.size() > 0) {
            for (Map.Entry<String, String> entry : header.entrySet()) {
                httpGet.setHeader(entry.getKey(), entry.getValue());
            }
        }
        CloseableHttpResponse closeableHttpResponse = closeableHttpClient.execute(httpGet);
        HttpEntity entity = closeableHttpResponse.getEntity();
        String content = EntityUtils.toString(entity, encoding);
        int statusCode = closeableHttpResponse.getStatusLine().getStatusCode();
        if (statusCode == 200) {
            log.info(content);
            return content;
        } else {
            log.error("request exception, http reponse code:" + statusCode);
            return null;
        }

    }

    public String doGet(String url, Map<String, String> header, Map<String, Object> params, String encoding) throws URISyntaxException, IOException {
        if (null == encoding) {
            encoding = "utf-8";
        }
        URIBuilder uriBuilder = new URIBuilder(url);
        if (null != params && params.size() > 0) {
            for (Map.Entry<String, Object> entry : params.entrySet()) {
                uriBuilder.setParameter(entry.getKey(), entry.getValue().toString());
            }
        }
        return doGet(uriBuilder.build().toString(), header, encoding);
    }

    public String doPost(String url, Map<String, String> header, Map<String, Object> requestParameter, String encoding) throws Exception {
        if (null == encoding) {
            encoding = "utf-8";
        }
        HttpPost httpPost = new HttpPost(url);
        httpPost.setConfig(config);
        if (null != header && header.size() > 0) {
            for (Map.Entry<String, String> entry : header.entrySet()) {
                httpPost.setHeader(entry.getKey(), entry.getValue());
            }
        }
        if (null != requestParameter && requestParameter.size() > 0) {
            String requestBody = JSONObject.toJSONString(requestParameter);
            StringEntity postEntity = new StringEntity(requestBody, encoding);
            httpPost.setEntity(postEntity);
        }
        CloseableHttpResponse response = this.closeableHttpClient.execute(httpPost);
        // 对请求的响应进行简单的包装成自定义的类型
        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
            String result = EntityUtils.toString(response.getEntity(), encoding);
            log.info("" + response.getStatusLine().getStatusCode());
            return result;
        }
        return null;

    }

    public String doPost1(String url, Map<String, String> header, Map<String, Object> requestParameter, String encoding) throws Exception {
        if (null == encoding) {
            encoding = "utf-8";
        }
        HttpPost httpPost = new HttpPost(url);
        httpPost.setConfig(config);
        if (null != header && header.size() > 0) {
            for (Map.Entry<String, String> entry : header.entrySet()) {
                httpPost.setHeader(entry.getKey(), entry.getValue());
            }
        }
        if (null != requestParameter && requestParameter.size() > 0) {
            List<NameValuePair> list = new ArrayList<NameValuePair>();
            for (Map.Entry<String, Object> entry : requestParameter.entrySet()) {
                list.add(new BasicNameValuePair(entry.getKey(), entry.getValue().toString()));
            }
            // 构造form表单对象
            UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(list, encoding);

            // 把表单放到post里
            httpPost.setEntity(urlEncodedFormEntity);
        }
        CloseableHttpResponse response = this.closeableHttpClient.execute(httpPost);
        // 对请求的响应进行简单的包装成自定义的类型
        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
            String result = EntityUtils.toString(response.getEntity(), encoding);
            return result;
        }
        return null;

    }

    public String doPost(String url, String encoding) throws Exception {
        return doPost(url, null, null, encoding);

    }


}
